/*
 * A simple ToDo Manager
 *
 * Copyright (c) 2011 Nosheen Zaza, Manuel Rebollo, Seyedsaeid Mahpeikar and
 * Wo Ke
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

package todomanager.ui.dialogs;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.GridLayout;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

import java.util.ResourceBundle;
import todomanager.ui.ActionControl;
import com.toedter.calendar.JDateChooser;


/**
 * An instance of this class shows a dialog box used to add new tasks.
 * @author
 * Nosheen Zaza
 */
public class NewTaskDialog extends JDialog {

    private JLabel addTaskLabel;
    private JLabel categoryLabel;
    private JLabel priorityLabel;
    private JLabel dateLabel;

    private JTextArea taskTextArea;
    private JScrollPane taskScroll;
    
    private JDateChooser dateChooser;
    
    private JButton addButton;
   
    private JComboBox categoryComboBox;
    private JComboBox priorityComboBox;

    private JPanel addLabelPanel;
    private JPanel taskTextPanel;
    private JPanel choicesPanel;
    private JPanel buttonPanel;
    private JPanel optionsPanel;
    

    private ResourceBundle res;

    /**
     * Creates an instance and initializes its components.
     */
    public NewTaskDialog() {

        res = ActionControl.getInstance().getStringsBundle();

        addTaskLabel = new JLabel(res.getString("addATaskLabel"));

        addLabelPanel = new JPanel();
        addLabelPanel.add(addTaskLabel);

        taskTextArea = new JTextArea();
        taskTextArea.setLineWrap(true);
        taskTextArea.setWrapStyleWord(true);
        taskTextArea.setFocusable(true);

        taskScroll = new JScrollPane(taskTextArea);
        taskScroll.setBorder(BorderFactory.createEmptyBorder(5, 10, 5, 10));

        taskTextPanel = new JPanel();
        taskTextPanel.setLayout(new BoxLayout(taskTextPanel, BoxLayout.PAGE_AXIS));
        taskTextPanel.add(taskScroll);
        
        categoryLabel = new JLabel(res.getString("categoryLabel"));

        String[] categories = res.getString("Categories").split(",");
        categoryComboBox = new JComboBox(categories);

        priorityLabel = new JLabel(res.getString("priorityLabel"));
        
        String[] priorities = res.getString("Priorities").split(",");
        priorityComboBox = new JComboBox(priorities);  
        priorityComboBox.setSelectedIndex(1);


        dateLabel = new JLabel(res.getString("date"));
        dateChooser = new JDateChooser();
        dateChooser.setDateFormatString("dd-MM-yyyy");

        addButton = new JButton();
        addButton.setAction(ActionControl.getInstance().getCreateTaskAction());
        buttonPanel = new JPanel();
        buttonPanel.add(addButton);

        choicesPanel = new JPanel(new GridLayout(3, 2, 5, 5));
        choicesPanel.add(categoryLabel);
        choicesPanel.add(categoryComboBox);
        choicesPanel.add(priorityLabel);
        choicesPanel.add(priorityComboBox);
        choicesPanel.add(dateLabel);
        choicesPanel.add(dateChooser);

        optionsPanel = new JPanel();
        optionsPanel.setLayout(new BoxLayout(optionsPanel, BoxLayout.PAGE_AXIS));
        optionsPanel.setBorder((BorderFactory.createEmptyBorder(0, 5, 0, 5)));
        optionsPanel.add(choicesPanel);
        optionsPanel.add(Box.createRigidArea(new Dimension(3, 5)));
        optionsPanel.add(buttonPanel);

        addWindowListener(new WindowAdapter() {

            @Override
            public void windowClosing(WindowEvent e) {
                // Empties the text box when this dialog box is closed
                NewTaskDialog.this.taskTextArea.setText("");
            }
        });

        getRootPane().setDefaultButton(addButton);
        setLayout(new BorderLayout());
        add(addLabelPanel,BorderLayout.PAGE_START);
        add(taskTextPanel, BorderLayout.CENTER);
        add(optionsPanel, BorderLayout.PAGE_END);
        setTitle(res.getString("addATaskLabel"));

        setSize(220, 250);
        setResizable(false);
        setLocationRelativeTo(ActionControl.getInstance().getMainWindow());
    }

    /**
     * Returns the text area that holds the task text.
     * @return
     */
    public JTextArea getTaskTextArea() {
        return taskTextArea;
    }

    /**
     * Returns the category combo box.
     * @return categoryComboBox
     */
    public JComboBox getCategoryComboBox() {
        return categoryComboBox;
    }

    /**
     * Returns the priority combo box.
     * @return priorityComboBox
     */
    public JComboBox getPriorityComboBox() {
        return priorityComboBox;
    }

    /**
     * Returns the date chooser component.
     * @return dateChooser
     */
    public JDateChooser getDateChooser() {
        return dateChooser;
    }
}
